﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace zdzh.zdmgr.UIL.UserControls
{
    /// <summary>
    /// 对象名称：导航栏类
    /// 对象说明：主要显示系统左侧的导航栏。
    /// 作者姓名：爱英思躺（QQ：2415540409）
    /// 编写日期：2010/05/29 23:57:06
    /// </summary>
    public partial class NavBar : System.Web.UI.UserControl
    {
        protected global::System.Web.UI.WebControls.Repeater RepTopBar;
        protected global::System.Web.UI.WebControls.Repeater RepButton;
        protected global::System.Web.UI.WebControls.Repeater RepBottomBar;

        protected void Page_Load(object sender, EventArgs e)
        {
            RepTopBar.DataSource = TopGroup;
            RepTopBar.DataBind();

            RepButton.DataSource = CurrentGroup.ImageButtons;
            RepButton.DataBind();

            RepBottomBar.DataSource = BottomGroup;
            RepBottomBar.DataBind();
        }

        /// <summary>
        /// 导航栏类的默认实例化方法，该方法在Page_Load（）之前执行，
        /// 可以根据需要在此方法中对 NavButtonGroup列表进行重新定义。
        /// </summary>
        public NavBar()
        {
            ////示例：根据用户角色修改导航条
            //if(!NowUser.IsAdministrator)                      //如果不是管理员
            //{
            //    NavButtonGroup.RemoveAt(2);                   //删除导航栏中第3组导般条
            //    NavButtonGroup[1].ImageButtons.RemoveAt(0);   //删除导航栏中第2组导般条中的第1个图像按钮
            //}

            InitNavBar();  //初始化导航栏，该方法必须在NavButtonGroup重新调整后执行
        }

        /// <summary>
        /// 说明：导航栏初始的功能模块列表，可以通过定义此列表设置导航栏中的导航条及图片按扭。
        /// 注意：这里定义的是一个私有的List类型变量NavButtonGroup，并不是一个私有的函数方法。
        /// 警告：每一个ButtonGroup对象里至少需要有一个ImageButton对象，否则绑定时将不予显示。
        /// </summary>
        private List<ButtonGroup> NavButtonGroup = new List<ButtonGroup>() 
        { 
            new ButtonGroup("正大中和")
            {
                ImageButtons = new List<ImageButton>() 
                {
                    new ImageButton("房源管理","fangyuanp","TmpPath.png"),
                    new ImageButton("订单","dingdanp","TmpPath.png"),
                    new ImageButton("销售","xiaoshoup","TmpPath.png")
                }
            } ,
            new ButtonGroup("翰隆物业")
            {
                ImageButtons = new List<ImageButton>() 
                {
                    new ImageButton("收费项目管理","wuyefeip","TmpPath.png"),
                    new ImageButton("空白模块","TmpPath","TmpPath.png")
                }
            } ,
            new ButtonGroup("回迁")
            {
                ImageButtons = new List<ImageButton>() 
                {
                    new ImageButton("回迁协议","huiqianxieyip","huiqianxieyip.png"),
                    new ImageButton("回迁户","huiqianhup","huiqianhup.png"),
                    new ImageButton("回迁电话","huiqiandianhuap","huiqiandianhuap.png"),
                    new ImageButton("回迁房照","huiqianfangzhaop","huiqianfangzhaop.png")
                }
            } ,
            new ButtonGroup("员工")
            {
                ImageButtons = new List<ImageButton>() 
                {
                    new ImageButton("员工","yuangongp","TmpPath.png")
                }
            }
        };

        #region 导航栏类中的相关属性
        /// <summary>
        /// [属性]获取导航栏中按钮区域上部的导航条列表
        /// </summary>
        private List<ButtonGroup> TopGroup
        {
            get
            {
                return NavButtonGroup.GetRange(0, CurrentGroupIndex+1);
            }
        }

        /// <summary>
        /// [属性]获取导航栏中当前选中的单个导航条对象
        /// </summary>
        private ButtonGroup CurrentGroup
        {
            get
            {
                return NavButtonGroup[CurrentGroupIndex];
            }
        }

        /// <summary>
        /// [属性]获取导航栏中按钮区域下部的导航条列表
        /// </summary>
        private List<ButtonGroup> BottomGroup
        {
            get
            {
                return NavButtonGroup.GetRange(CurrentGroupIndex + 1, NavButtonGroup.Count - CurrentGroupIndex-1);
            }
        }

        /// <summary>
        /// 当前导航栏中选中的导航条编号
        /// </summary>
        private int CurrentGroupIndex = 0;

        /// <summary>
        /// 初始化导航条：移除导航栏中没有按钮的导航条，并确定当前导航栏中选中的导航条编号
        /// </summary>
        private void InitNavBar()
        {
            CurrentGroupIndex = 0;
            string requestPath = System.Web.HttpContext.Current.Request.PhysicalPath;
            System.IO.DirectoryInfo moduleDir = new System.IO.DirectoryInfo(requestPath);
            string moduleRunPath = moduleDir.Parent.Name;
            for (int n = 0; n < NavButtonGroup.Count; n++)
            {
                foreach (ImageButton imageButton in NavButtonGroup[n].ImageButtons)
                {
                    if (imageButton.RunPath == moduleRunPath)
                        CurrentGroupIndex = n;
                }
                if (NavButtonGroup[n].ImageButtons.Count == 0) 
                {
                    NavButtonGroup.RemoveAt(n); //移除导航栏中没有按钮的导航条
                    n = 0;
                }
            }
        }
        #endregion

        #region 导航栏所需的相关对象

        /// <summary>
        /// 导航栏中的导航条
        /// </summary>
        public class ButtonGroup
        {
            private string _Name;
            private List<ImageButton> _ImageButtons;

            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }

            public List<ImageButton> ImageButtons
            {
                get { return _ImageButtons; }
                set { _ImageButtons = value; }
            }

            public ButtonGroup(string name)
            {
                this.Name = name;
            }
        }

        /// <summary>
        /// 导航栏中的图片按钮
        /// </summary>
        public class ImageButton
        {
            private string _Name;
            private string _RunPath;
            private string _ImageUrl;

            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }

            public string RunPath
            {
                get { return _RunPath; }
                set { _RunPath = value; }
            }

            public string ImageUrl
            {
                get { return _ImageUrl; }
                set { _ImageUrl = value; }
            }

            public ImageButton(string name, string runPath, string imageUrl)
            {
                this.Name = name;
                this.RunPath = runPath;
                this.ImageUrl = imageUrl;
            }
        }
        #endregion
    }
}
